//
//  RegisterViewController.m
//  DreamSoftReimburse
//
//  Created by ZJ on 17/4/13.
//  Copyright © 2017年 ZJ. All rights reserved.
//

#import "RegisterViewController.h"
#import "ZJAdditions.h"
#import "NetWorkingManager.h"
#import "UIButton+countDown.h"
#import "ZJReportViewController.h"
#import "ZJUserRegisterInfoModel.h"
//#import "LPChooseReportTypeViewController.h"
@interface RegisterViewController ()<UITextFieldDelegate>

/** 姓名 */
@property (nonatomic, strong) UITextField *name;
/** 手机号码 */
@property (nonatomic, strong) UITextField *phoneNumber;
/** 验证码 */
@property (nonatomic, strong) UITextField *verification;
/** 获取验证码 */
@property (nonatomic, strong) UIButton *verificationButton;
/** 注册 */
@property (nonatomic, strong) UIButton *userRegist;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"注册";
    self.view.backgroundColor = [UIColor zj_colorWithHexString:@"ffffff"];
    [self setupUI];
    
}
- (void)setupUI{
//    [self.view addSubview:self.name];
    [self.view addSubview:self.phoneNumber];
    [self.view addSubview:self.verification];
    [self.view addSubview:self.verificationButton];
    [self.view addSubview:self.userRegist];
    
    //姓名提示
//    UILabel *nameLabel = [UILabel new];
//    nameLabel.text = @"姓名 :";
//    nameLabel.textColor = [UIColor textBlackColor];
//    nameLabel.font = [UIFont titleTextFont];
    
    //手机号提示
    UILabel *phoneLabel = [UILabel new];
    phoneLabel.text = @"手机号 :";
    phoneLabel.textColor = [UIColor textBlackColor];
    phoneLabel.font = [UIFont titleTextFont];
    //验证码提示
    UILabel *VLabel = [UILabel new];
    VLabel.text = @"验证码 :";
    VLabel.textColor = [UIColor textBlackColor];
    VLabel.font = [UIFont titleTextFont];
//    [self.view addSubview:nameLabel];
    [self.view addSubview:phoneLabel];
    [self.view addSubview:VLabel];
    
//    nameLabel.sd_layout
//    .topSpaceToView(self.view, navHeight + 23 * [UIScreen zj_screenHScale])
//    .leftSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
//    .rightSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
//    .heightIs([NSString heightWithTextFontSize:[UIFont titleTextFontSize]]);
    
//    self.name.sd_layout
//    .topSpaceToView(nameLabel, 11 * [UIScreen zj_screenHScale])
//    .leftEqualToView(nameLabel)
//    .rightEqualToView(nameLabel)
//    .heightIs(44 * [UIScreen zj_screenHScale]);
    
    
    phoneLabel.sd_layout
    .topSpaceToView(self.view,navHeight + 23 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.view, 27 * [UIScreen zj_screenWScale])
    .heightIs([NSString heightWithTextFontSize:[UIFont titleTextFontSize]]);
    
    self.phoneNumber.sd_layout
    .topSpaceToView(phoneLabel, 11 * [UIScreen zj_screenHScale])
    .leftEqualToView(phoneLabel)
    .rightEqualToView(phoneLabel)
    .heightIs(44 * [UIScreen zj_screenHScale]);
    
    VLabel.sd_layout
    .topSpaceToView(self.phoneNumber, 23 * [UIScreen zj_screenHScale])
    .leftEqualToView(self.phoneNumber)
    .rightEqualToView(self.phoneNumber)
    .heightIs([NSString heightWithTextFontSize:[UIFont titleTextFontSize]]);
    
    self.verificationButton.sd_layout
    .topSpaceToView(VLabel, 11 * [UIScreen zj_screenHScale])
    .rightEqualToView(VLabel)
    .widthIs(100 * [UIScreen zj_screenWScale])
    .heightRatioToView(self.phoneNumber, 1);
    
    self.verification.sd_layout
    .topEqualToView(self.verificationButton)
    .leftEqualToView(VLabel)
    .rightSpaceToView(self.verificationButton, 12 * [UIScreen zj_screenWScale])
    .heightRatioToView(self.verificationButton, 1);
    
    self.userRegist.sd_layout
    .topSpaceToView(self.verification, 72 * [UIScreen zj_screenHScale])
    .leftEqualToView(self.verification)
    .rightEqualToView(self.verificationButton)
    .heightRatioToView(self.verificationButton, 1);
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    ZJReportViewController *vc = [ZJReportViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 点击事件
/** 发送验证码 */
- (void)action_sendVerification{
    [self.verification becomeFirstResponder];
    if (![CManager validateMobile:self.phoneNumber.text]) {
        [YJProgressHUD showMsgWithoutView:@"请输入正确的手机号码"];
        return;
    }
    __weak typeof(self) weakSelf = self;
    NSDictionary *dict = @{@"MOBILE":self.phoneNumber.text,@"CODE_SOURCE":@"register"};
    [NetWorkingManager postWithUrlString:[NetWorkingURL sendVeriCode] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"30000"]) {
            [weakSelf.verificationButton startWithTime:60 title:@"重新获取" countDownTitle:@"重新获取" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor]];
        }
    } Fail:^(id object) {
        
    }];
    
}



/** 注册 */
- (void)action_userRegist{
    [self.view endEditing:YES];
    __weak typeof(self) weakSelf = self;
    //输入的信息都正确
    if ([CManager zj_validateMobile:self.phoneNumber.text] && self.verification.text.length == 6) {
        //验证验证码
        
        [self verificationNumberSuccee:^(NSDictionary *dict){
            ZJUserRegisterInfoModel *model = [ZJUserRegisterInfoModel yy_modelWithJSON:dict];
            ZJReportViewController *vc = [ZJReportViewController new];
            vc.registerModel = model;
            [weakSelf.navigationController pushViewController:vc animated:YES];

        } Fail:^{
            [YJProgressHUD showMsgWithoutView:@"验证码输入错误"];
        }];
        return;
    }else{
        [YJProgressHUD showMsgWithoutView:@"请输入正确的姓名、手机号及验证码"];
    }
    }

#pragma mark - 验证验证码

- (void)verificationNumberSuccee:(void(^)(NSDictionary * dict))succee Fail:(void(^)())fail{
    [YJProgressHUD show:@"" inView:self.view mode:YJProgressModeLoading];
    NSDictionary *dict = @{@"MOBILE":self.phoneNumber.text,@"CODE_SOURCE":@"register",@"MOBILE_CODE":self.verification.text,@"USER_NAME":@""};
    [NetWorkingManager postWithUrlString:[NetWorkingURL registerUserInfo] Parameter:dict Success:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"30000"]) {//验证成功
                succee(object);
        }else if([model.STATE isEqualToString:@"30003"]){//30002 验证码错误 30003 改手机号已被注册 30001 服务器执行错误
            [YJProgressHUD showMsgWithoutView:@"手机号已经被注册"];
        }else if([model.STATE isEqualToString:@"30008"]){//30002 验证码错误 30003 改手机号已被注册 30001 服务器执行错误 30008 未找到党员信息
            [YJProgressHUD showMsgWithoutView:@"未找到党员信息"];
        }else{
         fail();
        }
    } Fail:^(id object) {
        [YJProgressHUD showMsgWithoutView:@"验证码错误"];
    }];

}

#pragma mark - UITextFieldDelegate 协议方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField == self.phoneNumber) {
        if (toBeString.length >= 11 && range.length!=1){
            textField.text = [toBeString substringToIndex:11];
            return NO;
            
        }
    }else if (textField == self.verification) {
        if (toBeString.length >= 6 && range.length!=1){
            textField.text = [toBeString substringToIndex:6];
            return NO;
            
        }
    }
    
    return YES;
}

#pragma mark - Setter
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}

#pragma mark - Getters

- (UITextField *)name{
    if (_name == nil){
        _name = [[UITextField alloc]init];
        _name.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _name.placeholder = @"请输入姓名";
        _name.textColor = [UIColor textBlackColor];
        _name.font = [UIFont titleTextFont];
        [_name setTextFieldLeftforWidth:14 * [UIScreen zj_screenWScale]];
        [_name setTextFieldRightforWidth:14 * [UIScreen zj_screenWScale]];
        _name.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        _name.layer.borderWidth = 1.0;
        _name.sd_cornerRadius = @(4 * [UIScreen zj_screenHScale]);
        _name.returnKeyType = UIKeyboardTypeTwitter;
        _name.delegate = self;
    }
    return _name;
}

- (UITextField *)phoneNumber{
    if (_phoneNumber == nil){
        _phoneNumber = [[UITextField alloc]init];
         _phoneNumber.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _phoneNumber.placeholder = @"请输入手机号";
        _phoneNumber.textColor = [UIColor textBlackColor];
        _phoneNumber.font = [UIFont titleTextFont];
        [_phoneNumber setTextFieldLeftforWidth:14 * [UIScreen zj_screenWScale]];
        [_phoneNumber setTextFieldRightforWidth:14 * [UIScreen zj_screenWScale]];
        _phoneNumber.delegate = self;
        _phoneNumber.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        _phoneNumber.layer.borderWidth = 1.0;
        _phoneNumber.sd_cornerRadius = @(4 * [UIScreen zj_screenHScale]);
        _phoneNumber.returnKeyType = UIKeyboardTypeTwitter;
       
    }
    return _phoneNumber;
}

- (UITextField *)verification{
    if (_verification  == nil){
        _verification = [[UITextField alloc]init];
        _verification.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _verification.placeholder = @"请输入验证码";
        _verification.textColor = [UIColor textBlackColor];
        _verification.font = [UIFont titleTextFont];
        [_verification setTextFieldLeftforWidth:14 * [UIScreen zj_screenWScale]];
        _verification.delegate = self;
        [_verification setTextFieldRightforWidth:14 * [UIScreen zj_screenWScale]];
        _verification.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        _verification.layer.borderWidth = 1.0;
        _verification.sd_cornerRadius = @(4 * [UIScreen zj_screenHScale]);
        _verification.returnKeyType = UIKeyboardTypeTwitter;
        
    }
    return _verification;
}

- (UIButton *)verificationButton{
    if (_verificationButton == nil){
        _verificationButton = [[UIButton alloc]init];
        [_verificationButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_verificationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _verificationButton.titleLabel.font = [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale]];
        _verificationButton.backgroundColor = [UIColor themeColor];
        _verificationButton.layer.cornerRadius = 4 * [UIScreen zj_screenHScale];
        [_verificationButton addTarget:self action:@selector(action_sendVerification) forControlEvents:UIControlEventTouchUpInside];
    }
    return _verificationButton;
}
- (UIButton *)userRegist{
    if (_userRegist == nil){
        _userRegist = [[UIButton alloc]init];
        [_userRegist setTitle:@"立即注册" forState:UIControlStateNormal];
        [_userRegist setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _userRegist.backgroundColor = [UIColor themeColor];
        _userRegist.titleLabel.font = [UIFont titleTextFont];
        _userRegist.layer.cornerRadius = 4 * [UIScreen zj_screenHScale];
        [_userRegist addTarget:self action:@selector(action_userRegist) forControlEvents:UIControlEventTouchUpInside];
    }
    return _userRegist;
}

@end
